//
//  RegisterView.m
//  afn
//
//  Created by mac on 2018/5/9.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "RegisterView.h"

@implementation RegisterView

-(void)creatView{
    
    UILabel *label=[UILabel new];
    [self addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(50);
    }];
    label.text=@"立即注册，开启全新体验";
    label.font=[UIFont systemFontOfSize:16];
    label.textColor=ColorWithRGB(49, 49, 49);
    
    
    UIView *xianView=[UIView new];
    [self addSubview:xianView];
    [xianView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.bottom.mas_equalTo(self.mas_centerY).mas_equalTo(-70);
        make.height.mas_equalTo(1);
    }];
    xianView.backgroundColor=_LightGrey;
    
    _codeText=[UITextField new];
    [self addSubview:_codeText];
    [_codeText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(xianView);
        make.right.mas_equalTo(-120);
        make.bottom.mas_equalTo(xianView.mas_top).mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    _codeText.delegate=self;
    _codeText.placeholder=@"验证码";
    _codeText.font=[UIFont systemFontOfSize:15];
    _codeText.returnKeyType=UIReturnKeyDone;
    
    _codeBnt=[UIButton new];
    [self addSubview:_codeBnt];
    [_codeBnt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(self->_codeText);
        make.width.mas_equalTo(90);
        make.height.mas_equalTo(30);
    }];
    [_codeBnt setTitle:@"获取验证码" forState:0];
    [_codeBnt setTitleColor:ColorWithRGB(49, 49, 49) forState:0];
    _codeBnt.titleLabel.font=[UIFont boldSystemFontOfSize:11];
    _codeBnt.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [_codeBnt addTarget:self action:@selector(huoQuYanZhen:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    UIView *xianView2=[UIView new];
    [self addSubview:xianView2];
    [xianView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.bottom.mas_equalTo(self->_codeText.mas_top).mas_equalTo(-15);
        make.height.mas_equalTo(1);
    }];
    xianView2.backgroundColor=_LightGrey;
    
    _phoneText=[UITextField new];
    [self addSubview:_phoneText];
    [_phoneText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(xianView2);
        make.right.mas_equalTo(-120);
        make.bottom.mas_equalTo(xianView2.mas_top).mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    _phoneText.placeholder=@"手机号";
    _phoneText.font=[UIFont systemFontOfSize:15];
    _phoneText.keyboardType=UIKeyboardTypePhonePad;
    
    
    _tuiJianText=[UITextField new];
    [self addSubview:_tuiJianText];
    [_tuiJianText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(xianView);
        make.right.mas_equalTo(-120);
        make.top.mas_equalTo(xianView.mas_bottom).mas_equalTo(15);
        make.height.mas_equalTo(50);
    }];
    _tuiJianText.placeholder=@"推荐人手机号";
    _tuiJianText.font=[UIFont systemFontOfSize:15];
    _tuiJianText.keyboardType=UIKeyboardTypePhonePad;
    
    UILabel *label4=[UILabel new];
    [self addSubview:label4];
    [label4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(self->_tuiJianText);
    }];
    label4.text=@"选填";
    label4.font=[UIFont systemFontOfSize:11];
    label4.textColor=ColorWithRGB(149, 149, 149);
    
    UIView *xianView3=[UIView new];
    [self addSubview:xianView3];
    [xianView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(self->_tuiJianText.mas_bottom);
        make.height.mas_equalTo(1);
    }];
    xianView3.backgroundColor=_LightGrey;
    
    
    UILabel *label3=[UILabel new];
    [self addSubview:label3];
    [label3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.mas_equalTo(xianView3.mas_bottom).mas_equalTo(5);
    }];
    label3.text=@"(填写可获得10元优惠券)";
    label3.font=[UIFont systemFontOfSize:11];
    label3.textColor=_backgroundColor;


    UIButton *LoginBtn=[UIButton new];
    [self addSubview:LoginBtn];
    [LoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(xianView3);
        make.top.mas_equalTo(xianView3.mas_bottom).mas_equalTo(50);
        make.height.mas_equalTo(50);
    }];
    LoginBtn.backgroundColor=_backgroundColor;
    [LoginBtn setTitleColor:[UIColor whiteColor] forState:0];
    [LoginBtn setTitle:@"注册" forState:0];
    LoginBtn.layer.cornerRadius=25;
    LoginBtn.titleLabel.font=[UIFont systemFontOfSize:16];
    [LoginBtn addTarget:self action:@selector(Register:) forControlEvents:UIControlEventTouchUpInside];




    UILabel *label2=[UILabel new];
    [self addSubview:label2];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-30);
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-15);
    }];
    label2.numberOfLines=0;
    label2.textAlignment=NSTextAlignmentCenter;
    [label layoutIfNeeded];

    NSMutableAttributedString * attributedStr = [[NSMutableAttributedString alloc] initWithString:@"我已审慎阅读并同意《法之春用户协议》、《法律严明》、《隐私政策》，接受免除或限制责任、诉讼管辖约定"];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:ColorWithRGB(149, 149, 149) range:NSMakeRange(0, 9)];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:ColorWithRGB(99, 161, 252) range:NSMakeRange(9, 9)];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:ColorWithRGB(149, 149, 149) range:NSMakeRange(18, 1)];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:ColorWithRGB(99, 161, 252) range:NSMakeRange(19, 6)];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:ColorWithRGB(149, 149, 149) range:NSMakeRange(25, 1)];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:ColorWithRGB(99, 161, 252) range:NSMakeRange(26, 6)];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:ColorWithRGB(149, 149, 149) range:NSMakeRange(32, 17)];
    label2.attributedText=attributedStr;
    label2.font=[UIFont systemFontOfSize:11];

    UIImageView *imageView=[UIImageView new];
    [self addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(label2.mas_left).mas_equalTo(0);
        make.top.mas_equalTo(label2);
        make.width.height.mas_equalTo(13);
    }];
    imageView.image=[UIImage imageNamed:@"ico-gouxuan-on"];
}

-(IBAction)huoQuYanZhen:(UIButton *)sender{
    
    NSString *mobile=_phoneText.text;
    if (![gongju isMobileNumber:mobile]) {
        [gongju svpInfo:@"手机号错误"];
        return;
    }
    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:mobile,@"mobile",@"register",@"type", nil];
    [gongju AFNPOSTDataUrl:@"v1/smscode/get-sms-code" andDic:dic andTableView:nil andViewController:self.viewController andSuccessBlock:^(id responseObject) {
        self->_codeBnt.enabled=NO;
        self->_timeTick=60;
        self->_timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
    } anderrorBlock:nil];

}


-(void)timeFireMethod
{
    if (_timeTick==0) {
        _codeBnt.enabled=YES;
        [_codeBnt setTitle:@"发送验证码" forState:0];
        [_codeBnt setTitleColor:ColorWithRGB(49, 49, 49) forState:0];
        [_timer invalidate];
        return;
    }
    _timeTick--;
    [_codeBnt setTitle:[NSString stringWithFormat:@"%dS重发",_timeTick] forState:0];
    [_codeBnt setTitleColor:ColorWithRGB(149, 149, 149) forState:0];
}

-(IBAction)Register:(UIButton *)sender{
    NSString *mobile=_phoneText.text;
    if (![gongju isMobileNumber:mobile]) {
        [gongju svpInfo:@"手机号错误"];
        return;
    }
    NSString *codeStr=_codeText.text;
    if ([gongju charIsNil:codeStr]) {
        [gongju svpInfo:@"验证码不能为空"];
        return;
    }
    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:mobile,@"mobile",codeStr,@"code", nil];
    [gongju AFNPOSTDataUrl:@"v1/register/register" andDic:dic andTableView:nil andViewController:self.viewController andSuccessBlock:^(id responseObject) {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:[responseObject objectForKey:@"token"] forKey:@"token"];
    } anderrorBlock:nil];
    
    
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string{
    if ([[[UITextInputMode currentInputMode]primaryLanguage] isEqualToString:@"emoji"]) {
        return NO;
    }
    return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}


@end
